package net.huashitong.Project.web;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Project.entity.NewProjects;
import net.huashitong.Project.service.NewProjectsService;
import net.huashitong.log.service.LogMessageService;
import net.huashitong.node.entity.NodeNewschedules;
import net.huashitong.node.service.NodeNewschedulesService;
import net.huashitong.node.service.NodeSchedulesService;
import net.huashitong.user.entity.BnpUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static net.huashitong.utils.UtilsTools.getClientIp;

@Controller
@RequestMapping(value = "/projects")
public class ProjectInfoController extends JsonBaseController {

    @Override
    protected String getView(String s) {
        return null;
    }

    @Lazy
    @Autowired
    private LogMessageService logMessageService;
    @Autowired
    private NewProjectsService newProjectsService;
    @Autowired
    private NodeNewschedulesService nodeNewschedulesService;

    @ResponseBody
    @RequestMapping(value = "/getprojectinfo", method = RequestMethod.GET)
    public Map<String, String> getProjectInfo(String projectId) {
        Map<String, String> projectMap = new HashMap<String, String>();
        NewProjects newProject = newProjectsService.getById(projectId);
        projectMap.put("name", newProject.getName()); //项目名称
        projectMap.put("resDept", newProject.getResDept());  //责任单位
        projectMap.put("resUser", newProject.getResUser());  //分管领导
        return projectMap;
    }

    /**
     * 修改项目
     *
     * @param obj
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editProjectInfo", method = RequestMethod.POST)
    public Message editProjectInfo(@ModelAttribute NewProjects obj, HttpServletRequest request) throws NoSuchFieldException {
        String ip = getClientIp(request);
        BnpUser user = (BnpUser) WebUtils.getSessionAttribute(request, "user");
        NewProjects newProject = newProjectsService.getById(obj.getSysId());
        Map<String, Object> isEdit = newProjectsService.updateProjectInfo(newProject, obj, ip, user);
        boolean success = (boolean) isEdit.get("success");
        if (!success) {
            return coverMessage("500", (String) isEdit.get("msg"));
        }
        //征地拆迁节点更新
        NodeNewschedules nnsList = nodeNewschedulesService.getNnsByProjectIdAndNodeId(obj.getSysId(), "5.3");
        NodeNewschedules nnsList1 = nodeNewschedulesService.getNnsByProjectIdAndNodeId(obj.getSysId(), "23.1");
        if (ObjectUtils.isNotBlank(nnsList)) {
            nnsList.setResDept(obj.getOccupyDept());
            nnsList.setResUser(obj.getOccupyResUser());
            nnsList.setResUserPhone(obj.getOccupyResUserPhone());
            nnsList.setOperatorUser(obj.getOccupyOperateUser());
            nnsList.setOperatorPhone(obj.getOccupyOperateUserPhone());
            nodeNewschedulesService.update(nnsList);
        }
        if (ObjectUtils.isNotBlank(nnsList1)) {
            nnsList1.setResDept(obj.getOccupyDept());
            nnsList1.setResUser(obj.getOccupyResUser());
            nnsList1.setResUserPhone(obj.getOccupyResUserPhone());
            nnsList1.setOperatorUser(obj.getOccupyOperateUser());
            nnsList1.setOperatorPhone(obj.getOccupyOperateUserPhone());
            nodeNewschedulesService.update(nnsList1);
        }
        return coverMessage("200");
    }

    @RequestMapping(value = "/getProjectKView", method = RequestMethod.GET)
    public String getProjectKView(Model model, HttpServletResponse response, HttpServletRequest request) {
        return "/modules/index/index_chart_4";
    }


}
